package com.arsl.client.presenter;

import com.arsl.client.PaperaziServiceAsync;
import com.arsl.client.data.Content;
import com.arsl.client.data.Paper;
import com.arsl.client.data.Tag;
import com.arsl.client.util.Constants;
import com.arsl.client.util.Constants.ContentStatus;
import com.arsl.client.util.Utils;
import com.arsl.client.view.PageTagger;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;

public class PageTaggerPresenter implements Presenter {

	private PaperaziServiceAsync rpcService;
	private PageTagger view;
	private HandlerManager eventBus;
	private HasWidgets contentPanel;

	public PageTaggerPresenter(PaperaziServiceAsync rpcService,
			HandlerManager eventBus, PageTagger view) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.view = view;
		view.setPresenter(this);
	}

	@Override
	public void go(HasWidgets container) {
		contentPanel = container;
		contentPanel.clear();
		intiControls();
		contentPanel.add(view);
	}

	private void intiControls() {
		view.getPaperDetails();
	}

	public void getPaperDetails(int paperId) {
		rpcService.getPaperDetails(paperId, new AsyncCallback<Paper>() {

			@Override
			public void onSuccess(Paper result) {
				// TODO Auto-generated method stub
				float[] newDimensions = Utils.getresizedDimensions(
						result.getHeight(), result.getWidth(),
						Constants.DEFAULT_PAGE_IMAGE_WIDTH);
				view.setResizedWidth(newDimensions[0]);
				view.setResizedHeight(newDimensions[1]);
				view.setPaper(result);
				view.tryLoadExistingImage();
				view.setCanvas();
				view.loadTags();
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
		});
	}

	public void getPageImageUrIfExists(int arPageId) {
		rpcService.getPageImageUrl(arPageId, new AsyncCallback<String>() {

			@Override
			public void onSuccess(String imageUrl) {
				view.setImage(imageUrl);

				// view.pageCanvas = new
				// DrawingArea((int)view.getResizedWidth(),
				// (int)view.getResizedHeight());
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				caught.printStackTrace();
			}
		});
	}

	public void getPaperTags(int arPageId) {
		rpcService.getTags(arPageId, new AsyncCallback<Tag[]>() {

			@Override
			public void onSuccess(Tag[] result) {
				view.drawTags(result);
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());

			}
		});
	}

	public void getTagContent(int tagId) {
		rpcService.getContent(tagId, new AsyncCallback<Content[]>() {

			@Override
			public void onSuccess(Content[] result) {
				for (Content content : result) {
					content.setUUID(Utils.randomUUID());
					content.setStatus(ContentStatus.Update);
				}
				view.loadTagContent(result);

			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
		});
	}

	public void addTag(Integer ARPageId, float startx, float starty,
			float height, float width) {

		rpcService.addTag(ARPageId, startx, starty, height, width,
				new AsyncCallback<Integer>() {

					@Override
					public void onSuccess(Integer tagId) {
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
	}

	public void updateTagContent(Integer tagId, Content[] contents) {

		rpcService.updateTagContent(tagId, contents,
				new AsyncCallback<Integer>() {
					@Override
					public void onSuccess(Integer tagId) {
						view.submitCompleted();
					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}
				});
	}

}
